package com.rx.substation.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rx.substation.dao.MonthCompleteStatusDAO;
import com.rx.substation.domain.RunUnitCheck;
@Service
public class MonthCompleteStatusService {
	
	@Autowired
	MonthCompleteStatusDAO monthCompleteStatusDAO;

	/**
	 * 得到站点月完成情况
	 * @param start
	 * @param end
	 * @param stationCode
	 * @return
	 */
	public Map<String, Object> getMonthCompleteStatus(String start, String end,String stationCode) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date lastMonth = null;
		try {
			lastMonth = sdf.parse(end);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		Map<String,Object> work =  new HashMap<>();
		Calendar c = Calendar.getInstance();
        c.setTime(lastMonth);
        int getyear = c.get(Calendar.YEAR);
        int getmonth = c.get(Calendar.MONTH)+1;
		
        System.out.println(start+"^^^^^^^^^^^^^^^"+getmonth+"^^^^^^^^^^^^"+getyear+"^^^^^^^^^^^^^^^^^^^^^"+end+"^^^^^^^^^^^^^^^");
        
		List<Map<String,Object>> ws = monthCompleteStatusDAO.getWeekCompleteStatus(start,end,stationCode);
		if(ws.size() > 0){
			for (int i = 0; i < ws.size(); i++) {
				Map<String, Object> map = ws.get(i);
				for(String k : map.keySet()){
					System.out.println(k+":"+map.get(k));
					String time = map.get("createDate").toString();
					SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
					try {
						time = sdf1.format(sdf1.parse(time));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					String lastmonth = sdf.format(lastMonth);
					if(time.indexOf(lastmonth) != -1){
						work.put("weektime"+i, time);
					}else{
						work.put("weektime"+i, "1990-01-01");
					}
				}
			}
		}else{
			for (int i = 0; i < 5; i++) {
				work.put("weektime"+i, "1900-01-01");
			}
		}
		
		
		Calendar cal = Calendar.getInstance();
    	//获得上上个月月初
    	cal.add(Calendar.MONTH, -2);
	    cal.set(Calendar.DATE, 1);
	    String start1 = sdf.format(cal.getTime());
		
		List<Map<String,Object>> ms = monthCompleteStatusDAO.getMonthCompleteStatus(start1,end,stationCode);
		if(ms.size() == 2){
			for (int i = 0; i < ms.size(); i++) {
				Map<String, Object> map = ms.get(i);
				for(String k : map.keySet()){
					String time = map.get("createDate").toString();
					SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
					try {
						time = sdf1.format(sdf1.parse(time));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					work.put("monthtime"+i, time);
				}
			}
		}else if(ms.size() == 1){
			
				Map<String, Object> map = ms.get(0);
				for(String k : map.keySet()){
					String time = map.get("createDate").toString();
					SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
					try {
						time = sdf1.format(sdf1.parse(time));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					work.put("monthtime1", time);
					work.put("monthtime0", "1900-01-01");
				}
			
		}else{
			work.put("monthtime0", "1900-01-01");
			work.put("monthtime1", "1900-01-01");
		}
		if(getmonth == 6 ){
			int year = getyear;
			String start2 = year+"-12-01";
			List<Map<String,Object>> hys = monthCompleteStatusDAO.getHalfYearCompleteStatus(start2,end,stationCode);
			System.out.println("-----------------6----半年报-----------------------------");
			if(hys.size() == 2){
				for (int i = 0; i < hys.size(); i++) {
					Map<String, Object> map = hys.get(i);
					for(String k : map.keySet()){
						String time = map.get("createDate").toString();
						SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
						try {
							time = sdf1.format(sdf1.parse(time));
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						work.put("halfyear"+i, time);
					}
				}
			}else if(hys.size() == 1){
				Map<String, Object> map = hys.get(0);
				
				String time = map.get("createDate").toString();
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
				try {
					time = sdf1.format(sdf1.parse(time));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				work.put("halfyear1", time);
				work.put("halfyear0", "1900-01-01");
			}else{
				work.put("halfyear0", "1900-01-01");
				work.put("halfyear1", "1900-01-01");
			}
		}else if(getmonth == 12 ){
			int year = getyear;
			String start2 = year+"-01-01";
			List<Map<String,Object>> hys = monthCompleteStatusDAO.getHalfYearCompleteStatus(start2,end,stationCode);
			System.out.println("-----------------12----半年报-----------------------------");
			if(hys.size() == 2){
				for (int i = 0; i < hys.size(); i++) {
					Map<String, Object> map = hys.get(i);
					for(String k : map.keySet()){
						String time = map.get("createDate").toString();
						SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
						try {
							time = sdf1.format(sdf1.parse(time));
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						work.put("halfyear"+i, time);
					}
				}
			}else if(hys.size() == 1){
				Map<String, Object> map = hys.get(0);
				
				String time = map.get("createDate").toString();
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
				try {
					time = sdf1.format(sdf1.parse(time));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				work.put("halfyear1", time);
				work.put("halfyear0", "1900-01-01");
			}else{
				work.put("halfyear0", "1900-01-01");
				work.put("halfyear1", "1900-01-01");
			}
			
		}else{
			work.put("halfyear0", "1900-01-01");
			work.put("halfyear1", "1900-01-01");
		}
		if(getmonth== 12){
			String start3 = getyear-1+"12-01";
			List<Map<String,Object>> ys = monthCompleteStatusDAO.getYearCompleteStatus(start3,end,stationCode);
			System.out.println("---------------------年报-----------------------------");
			if(ys.size() == 2){
				for (int i = 0; i < ys.size(); i++) {
					Map<String, Object> map = ys.get(i);
					for(String k : map.keySet()){
						String time = map.get("createDate").toString();
						SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
						try {
							time = sdf1.format(sdf1.parse(time));
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						work.put("year"+i, time);
					}
				}
			}else if(ys.size() == 1){
				Map<String, Object> map = ys.get(0);
				
				String time = map.get("createDate").toString();
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
				try {
					time = sdf1.format(sdf1.parse(time));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				work.put("year0", time);
				work.put("year1", "1900-01-01");
			}else{
				work.put("year0", "1900-01-01");
				work.put("year1", "1900-01-01");
			}
		}else{
			work.put("year0", "1900-01-01");
			work.put("year1", "1900-01-01");
		}
		
		return work;
	}

	public List<RunUnitCheck> findMonthCompleteStatus(String start, String end) {
		return monthCompleteStatusDAO.findMonthCompleteStatus(start,end);
	}

	public Map<String, Object> findCompleteStatusByConditions(String runUnitId, String cityId, String stationTypeId,
			String start, String end, String stationCode) {
		
		return monthCompleteStatusDAO.findCompleteStatusByConditions(runUnitId,cityId,stationTypeId,start,end,stationCode);
	}

}
